/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.component.page.old;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;

import javax.swing.JComponent;

import com.google.code.swingonfire.animation.Animation;
import com.google.code.swingonfire.animation.AnimationAfterStepConsumer;
import com.google.code.swingonfire.animation.AnimationFinishedConsumer;
import com.google.code.swingonfire.animation.AnimationStartingConsumer;
import com.google.code.swingonfire.animation.Animator;
import com.google.code.swingonfire.layout.FillLayout;

public abstract class AbstractJPageComponent extends JComponent implements AnimationStartingConsumer,
    AnimationAfterStepConsumer, AnimationFinishedConsumer
{

    private static final long serialVersionUID = -4772850292352004672L;

    private Component activeComponent;
    private Component previousComponent;
    private Component pendingComponent;

    private JPageAnimation animation;

    public AbstractJPageComponent()
    {
        super();

        super.setLayout(new FillLayout());
    }

    public Component getActiveComponent()
    {
        return activeComponent;
    }

    public Component getPreviousComponent()
    {
        return previousComponent;
    }

    public Component getPendingComponent()
    {
        return pendingComponent;
    }

    public JPageAnimation getAnimation()
    {
        return animation;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setLayout(LayoutManager mgr)
    {
        throw new UnsupportedOperationException("LayoutManager not supported");
    }

    protected void show(Component component)
    {
        if (activeComponent == component)
        {
            return;
        }

        if (activeComponent == null)
        {
            activeComponent = component;

            if (activeComponent != null)
            {
                prepareActiveComponent(activeComponent);
                activeComponent.setVisible(true);
                postpareActiveComponent(activeComponent);
            }

            validate();
            repaint();

            return;
        }

        pendingComponent = component;

        startAnimation();
    }

    protected abstract void startAnimation();

    protected void startAnimation(JPageAnimationFactory animationFactory)
    {
        if (animation != null)
        {
            return;
        }

        if (pendingComponent == null)
        {
            return;
        }

        previousComponent = activeComponent;
        activeComponent = pendingComponent;
        pendingComponent = null;

        if (animationFactory == null)
        {
            if (previousComponent != null)
            {
                preparePreviousComponent(previousComponent);
                previousComponent.setVisible(false);
                postparePreviousComponent(previousComponent);
            }

            if (activeComponent != null)
            {
                prepareActiveComponent(activeComponent);
                activeComponent.setVisible(true);
                postpareActiveComponent(activeComponent);
            }

            validate();
            repaint();
        }
        else
        {
            if (activeComponent != null)
            {
                activeComponent.setVisible(false);
                prepareActiveComponent(activeComponent);
                activeComponent.setSize(getSize());
            }

            validate();
            animation = animationFactory.createAnimation(this, activeComponent, previousComponent);
            Animator.start(animation, this);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void paint(Graphics graphics)
    {
        if (isOpaque())
        {
            graphics.setColor(getBackground());
            graphics.fillRect(0, 0, getWidth(), getHeight());
        }

        if (animation != null)
        {
            animation.renderAnimation((Graphics2D) graphics, getWidth(), getHeight());
        }
        else
        {
            super.paint(graphics);
        }
    }

    //    /**
    //     * {@inheritDoc}
    //     */
    //    @Override
    //    protected void paintChildren(Graphics graphics)
    //    {
    //        if (isOpaque())
    //        {
    //            graphics.setColor(getBackground());
    //            graphics.fillRect(0, 0, getWidth(), getHeight());
    //        }
    //
    //        if (animation != null)
    //        {
    //            animation.renderAnimation((Graphics2D) graphics, getWidth(), getHeight());
    //        }
    //        else
    //        {
    //            super.paintChildren(graphics);
    //        }
    //    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void animationStarting(Animation animation)
    {
        if (previousComponent != null)
        {
            previousComponent.setVisible(false);
            preparePreviousComponent(previousComponent);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void animationAfterStep(Animation animation, double time, double fraction)
    {
        repaint();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void animationFinished(Animation animation)
    {
        if (previousComponent != null)
        {
            postparePreviousComponent(previousComponent);
            previousComponent = null;
        }

        this.animation = null;

        if (activeComponent != null)
        {
            postpareActiveComponent(activeComponent);
            activeComponent.setVisible(true);
        }

        validate();
        repaint();

        startAnimation();
    }

    protected void prepareActiveComponent(Component activeComponent)
    {
    }

    protected void preparePreviousComponent(Component previousComponent)
    {
    }

    protected void postpareActiveComponent(Component activeComponent)
    {
    }

    protected void postparePreviousComponent(Component previousComponent)
    {
    }
}
